package com.service.pay.diamond.controller;

import com.service.pay.diamond.mapper.UserMapper;
import com.service.pay.diamond.vo.PayVO;
import com.service.sso.sdk.model.UserModel;
import com.service.sso.sdk.service.SsoService;
import com.service.sso.sdk.service.SsoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/")
public class IndexController {
    @Autowired
    private SsoUserService ssoUserService;
    @Autowired
    private SsoService ssoService;
    @Autowired
    private UserMapper userMapper;

    @GetMapping
    public String pay(ModelMap view) {
        PayVO vo = new PayVO();
        vo.setUserVO(userMapper.modelToVo(ssoUserService.getUser()));
        view.addAttribute("vo", vo);
        view.addAttribute("user", vo.getUserVO());
        return "pay";
    }

    @GetMapping("logout")
    public void logout(ModelMap view, HttpServletResponse response) throws IOException {
        UserModel userModel = ssoUserService.getUser();
        if (null != userModel) {
            ssoService.logout(userModel.getToken());
        }
        ssoUserService.logout();
        response.sendRedirect("/");
    }

}
